<?php
/**
 * @author 	Vu Nhat Lam
 * 			Mail: lamvnd@gmail.com
 * 			Mobile: 0984 52 62 68
 * @version 1.0.0
 */
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'btview');
define('LOCATION_BYPASS', 1);
define('NOPMPOPUP', 1);

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################
$id = intval($_POST['id']);
?>
<div class="threadlist" style="width: 99.8%;">
<?php
if ($_POST['do'] == 'files')
{
	$files = $db->query_read_slave("
		SELECT filename, size
		FROM " . TABLE_PREFIX . "torrentfiles
		WHERE torrentid = $id
		ORDER BY filename
	");
	?>
    <div class="threadlisthead table">
      <table width="100%" border="0" cellspacing="0">
        <tr>
          <td width="5%" style="padding: 3px; text-align: center;">#</td>
          <td style="padding: 3px;">Path</td>
          <td width="10%" style="padding: 3px; text-align: center;">Site</td>
        </tr>
      </table>
    </div>
    <ol class="threads">
	<?php
	$i = 0;
	while ($file = $db->fetch_array($files))
	{
		$i ++;
		?>
	  <li class="threadbit">
	    <div class="nonsticky">
	      <table width="100%" border="0" cellspacing="0">
	        <tr>
	          <td width="5%" style="padding: 3px; text-align: center;"><?php echo $i;?></td>
	          <td style="padding: 3px;"><?php echo $file['filename'];?></td>
	          <td width="10%" class="alt" style="padding: 3px; text-align: center;"><?php echo mksize($file['size']);?></td>
	        </tr>
	      </table>
	    </div>
	  </li>
		<?php
	}
	$db->free_result($files);
	?>
	</ol>
<?php
}
if ($_POST['do'] == 'seeders')
{
	$seeders = $db->query_read_slave("
		SELECT p.*, t.size
		FROM " . TABLE_PREFIX . "torrentpeers AS p
		JOIN " . TABLE_PREFIX . "torrent AS t USING(torrentid)
		WHERE p.torrentid = $id AND p.left = 0
		ORDER BY p.started
	");
	?>
	<div class="threadlisthead table">
      <table width="100%" border="0" cellspacing="0">
        <tr>
          <td style="padding: 3px;">Username</td>
          <td width="10%" style="padding: 3px; text-align: center;">Port</td>
          <td width="18%" style="padding: 3px; text-align: center;">Uploaded (Speed)</td>
          <td width="18%" style="padding: 3px; text-align: center;">Downloaded (Speed)</td>
          <td width="5%" style="padding: 3px; text-align: center;">Ratio</td>
          <td width="8%" style="padding: 3px; text-align: center;">%</td>
          <td width="10%" style="padding: 3px; text-align: center;">Connected</td>
          <td width="15%" style="padding: 3px; text-align: center;">Client</td>
        </tr>
      </table>
    </div>
    <ol class="threads">
	<?php
	while ($seeder = $db->fetch_array($seeders))
	{
		$ratio = get_ratio($seeder['uploaded'], $seeder['downloaded']);
		?>
	  <li class="threadbit">
	    <div class="nonsticky">
	      <table width="100%" border="0" cellspacing="0">
	        <tr>
	          <td style="padding: 3px;"><a href="member.php?u=<?php echo intval($seeder['userid']);?>"><?php echo $seeder['username'];?></a><?php if (can_moderate()) {echo '<br />' . $seeder['ip'];}?></td>
	          <td width="10%" class="alt" style="padding: 3px; text-align: center;"><?php echo $seeder['port'];?></td>
	          <td width="18%" style="padding: 3px; text-align: center;"><?php echo mksize($seeder['uploaded']);?> (<font color="#ff0000"><?php echo mksize($seeder['upspeed']);?>/s</font>)</td>
	          <td width="18%" class="alt" style="padding: 3px; text-align: center;"><?php echo mksize($seeder['downloaded']);?> (<span style="color:green;">S</span>)</td>
	          <td width="5%" style="padding: 3px; text-align: center;"><font color="<?php echo get_ratio_color($ratio)?>"><?php echo $ratio;?></font></td>
	          <td width="8%" class="alt" style="padding: 3px; text-align: center;"><?php echo sprintf('%.2f%%', 100 * (1 - ($seeder['left'] / $seeder['size'])));?></td>
	          <td width="10%" style="padding: 3px; text-align: center;"><?php echo vbdate($vbulletin->options['timeformat'], $seeder['started'])?>, <?php echo vbdate($vbulletin->options['dateformat'], $seeder['started'], true)?></td>
	          <td width="15%" class="alt" style="padding: 3px; text-align: center;"><?php echo getagent($seeder['useragent'], $seeder['peer_id']);?></td>
	        </tr>
	      </table>
	    </div>
	  </li>
		<?php
	}
	$db->free_result($seeders);
	?>
  	</ol>
<?php
}
if ($_POST['do'] == 'leechers')
{
	$leechers = $db->query_read_slave("
		SELECT p.*, t.size
		FROM " . TABLE_PREFIX . "torrentpeers AS p
		JOIN " . TABLE_PREFIX . "torrent AS t USING(torrentid)
		WHERE p.torrentid = $id AND p.left <> 0
		ORDER BY p.started
	");
	?>
	<div class="threadlisthead table">
	  <table width="100%" border="0" cellspacing="0">
		<tr>
		  <td style="padding: 3px;">Username</td>
		  <td width="10%" style="padding: 3px; text-align: center;">Port</td>
	      <td width="18%" style="padding: 3px; text-align: center;">Uploaded (Speed)</td>
		  <td width="18%" style="padding: 3px; text-align: center;">Downloaded (Speed)</td>
	      <td width="5%" style="padding: 3px; text-align: center;">Ratio</td>
		  <td width="8%" style="padding: 3px; text-align: center;">%</td>
	      <td width="10%" style="padding: 3px; text-align: center;">Connected</td>
		  <td width="15%" style="padding: 3px; text-align: center;">Client</td>
		</tr>
	  </table>
	</div>
	<ol class="threads">
	<?php
	while ($leecher = $db->fetch_array($leechers))
	{
		$ratio = get_ratio($leecher['uploaded'], $leecher['downloaded']);
		?>
	  <li class="threadbit">
	    <div class="nonsticky">
	      <table width="100%" border="0" cellspacing="0">
	        <tr>
	          <td style="padding: 3px;"><a href="member.php?u=<?php echo intval($leecher['userid']);?>"><?php echo $leecher['username'];?></a><?php if (can_moderate()) {echo '<br />' . $leecher['ip'];}?></td>
	          <td width="10%" class="alt" style="padding: 3px; text-align: center;"><?php echo $leecher['port'];?></td>
	          <td width="18%" style="padding: 3px; text-align: center;"><?php echo mksize($leecher['uploaded']);?> (<font color="#ff0000"><?php echo mksize($leecher['upspeed']);?>/s</font>)</td>
	          <td width="18%" class="alt" style="padding: 3px; text-align: center;"><?php echo mksize($leecher['downloaded']);?> (<font color="#ff0000"><?php echo mksize($leecher['downspeed']);?>/s</font>)</td>
	          <td width="5%" style="padding: 3px; text-align: center;"><font color="<?php echo get_ratio_color($ratio)?>"><?php echo $ratio;?></font></td>
	          <td width="8%" class="alt" style="padding: 3px; text-align: center;"><?php echo sprintf('%.2f%%', 100 * (1 - ($leecher['left'] / $leecher['size'])));?></td>
	          <td width="10%" style="padding: 3px; text-align: center;"><?php echo vbdate($vbulletin->options['timeformat'], $leecher['started'])?>, <?php echo vbdate($vbulletin->options['dateformat'], $leecher['started'], true)?></td>
	          <td width="15%" class="alt" style="padding: 3px; text-align: center;"><?php echo getagent($leecher['useragent'], $leecher['peer_id']);?></td>
	        </tr>
	      </table>
	    </div>
	  </li>
		<?php
	}
	$db->free_result($leechers);
	?>
  	</ol>
<?php
}
if ($_POST['do'] == 'complete')
{
	$completes = $db->query_read_slave("
		SELECT c.*
		FROM " . TABLE_PREFIX . "torrentcomplete AS c
		JOIN " . TABLE_PREFIX . "torrent AS t USING(torrentid)
		WHERE c.torrentid = $id
		ORDER BY c.dateline DESC
		LIMIT 10
	");
	?>
	<div class="threadlisthead table">
	  <table width="100%" border="0" cellspacing="0">
		<tr>
		  <td style="padding: 3px;">Username</td>
		  <td width="20%" style="padding: 3px; text-align: center;">Dateline</td>
		</tr>
	  </table>
	</div>
	<ol class="threads">
	<?php
	while ($complete = $db->fetch_array($completes))
	{
	?>
	  <li class="threadbit">
	    <div class="nonsticky">
	      <table width="100%" border="0" cellspacing="0">
	        <tr>
	          <td style="padding: 3px;"><a href="member.php?u=<?php echo intval($complete['userid']);?>"><?php echo $complete['username'];?></a></td>
	          <td width="20%" class="alt" style="padding: 3px; text-align: center;"><?php echo vbdate($vbulletin->options['timeformat'], $complete['dateline'])?>, <?php echo vbdate($vbulletin->options['dateformat'], $complete['dateline'], true)?></td>
	        </tr>
	      </table>
	    </div>
	  </li>
		<?php
	}
	$db->free_result($completes);
	?>
  	</ol>
<?php
}
?>
</div>
